package com.weilan.blog.admin.controller;


import com.weilan.blog.admin.service.BlogTagService;
import com.weilan.blog.admin.vo.BlogTagVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/9/27 10:41
 */
@Slf4j
@RestController

@RequestMapping("/blogTag")
public class BlogTagController {
    @Autowired
    private BlogTagService blogTagService;

    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody BlogTagVO blogTagVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取标签列表");
        return ResultUtil.result(SysConf.SUCCESS, blogTagService.getPageList(blogTagVO));
    }

    @AvoidRepeatableCommit
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody  BlogTagVO blogTagVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogTagService.addTag(blogTagVO);
    }


    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody  BlogTagVO blogTagVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);

        return blogTagService.editTag(blogTagVO);
    }


    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<BlogTagVO> tagVoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);

        return blogTagService.deleteBatchBlogTag(tagVoList);
    }

    @DeleteMapping("/deleteBatch/{ids}")
    public String delete(@Validated({Delete.class}) @PathVariable("ids") String [] ids) {


        return blogTagService.deleteBatchBlogTag(ids);
    }

    /**
     * 置顶博客
     * @param tagVO
     * @param result
     * @return
     */
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody BlogTagVO tagVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return blogTagService.stickBlogTag(tagVO);
    }

    /**
     * 根据点击次数进行排序
     * @return
     */
    @PostMapping("/tagSortByClickCount")
    public String tagSortByClickCount() {
        return blogTagService.tagSortByClickCount();
    }

    /**
     * 通过引用量排序标签
     * 引用量就是所有的文章中，有多少使用了该标签，如果使用的越多，该标签的引用量越大，那么排名越靠前
     *
     * @return
     */

    @PostMapping("/tagSortByCite")
    public String tagSortByCite() {
        return blogTagService.tagSortByCite();
    }

}
